package com.heima.freemarker.test;

import com.heima.freemarker.FreemarkerApp;
import com.heima.freemarker.entity.Student;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.FileWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SpringBootTest(classes = FreemarkerApp.class)
@RunWith(SpringRunner.class)
public class FreemarkerTest2 {
    @Autowired
    private Configuration configuration;

    @Test
    public void test() throws Exception {
        //1.通过模板文件，创建模板对象
        Template template = configuration.getTemplate("list.ftl");
        //2.准备数据
        Map<String, Object> map = getData();
        FileWriter fileWriter = new FileWriter("E:\\KuGou\\list.html");
        template.process(map, fileWriter);
    }

    private Map<String, Object> getData() {
        //遍历list标签数据
        Map<String, Object> map = new HashMap<>();
        Student student = new Student();
        student.setName("小红");
        student.setAge(19);
        student.setMoney(1000f);
        Student student1 = new Student();
        student1.setName("小美");
        student1.setAge(18);
        student1.setMoney(10000f);
        List<Student> list = new ArrayList<>();
        list.add(student);
        list.add(student1);
        map.put("stus", list);
        //遍历map标签数据
        Map<String, Student> studentMap = new HashMap<>();
        studentMap.put("stu1", student);
        studentMap.put("stu2", student1);
        map.put("stuMap", studentMap);
        return map;
    }
}
